# -*- coding: utf-8 -*-
class baseblock:

    def __init__ (self, elems):

        self.childs = []
        for i in elems:
            self.childs.append(i)
        
    def add (self, *block):
        for i in block:
            self.childs.append(i)

    def beginblock(self):
        return ("")

    def endblock(self):
        return ("")

    def tohtml (self):

        text = self.beginblock()

        for i in self.childs:
            text = text + i.tohtml()

        text = text + self.endblock()

        return text

class document(baseblock):
    
    def __init__ (self, title, base_path, *elems):
        baseblock.__init__(self, elems)
        self.title = title
        self.base_path = base_path

    def beginblock(self):
        text = """
<!-- -*- coding: utf-8; mode:html -*- -->
<!--
   Modelo de MC102
   Autor: Raphael Marcos Menderico
   Parte do Projeto EMECE
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <meta name="generator" content="Emacs"/>
  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  <meta http-equiv="Content-Style-Type" content="text/css"/>
  <link rel="stylesheet" type="text/css" href="%(path)sstyle.css"/>

  <title>%(title)s</title>
</head>
<body>
""" % {"title": self.title, "path": self.base_path}
        return (text)
        

    def endblock(self):
        text = """
<div class="rodape">

<p>

  <a rel="license"
   href="http://creativecommons.org/licenses/by-nc-sa/2.5/br/"><img
   alt="Creative Commons License" style="border-width:0"
   src="%(path)scc.png"/></a>

  <a href="http://validator.w3.org/check?uri=referer"><img
      src="%(path)svalid-xhtml10.png"
      style="border-width:0"
      alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a>

 <a href="http://jigsaw.w3.org/css-validator/">
  <img style="border:0;width:88px;height:31px"
       src="%(path)svcss.png" 
       alt="Valid CSS!" />
 </a>
</p>
<p>
   Esta
   obra está licenciada sob uma <a rel="license"
   href="http://creativecommons.org/licenses/by-nc-sa/2.5/br/">Licença
   Creative Commons</a>
</p>

<p>
<a href="http://www.lsd.ic.unicamp.br/~rmm/emece/autores.html">Autores desse documento</a>.
</p>

</div>
</body>
</html>
""" % {"path":self.base_path}
        return (text)

class div(baseblock):
    def __init__ (self, name, *elems):
        baseblock.__init__(self, elems)
        self.name = name

    def beginblock (self):
        return ("<div class=\"%s\">\n" % (self.name))

    def endblock (self):
        return ("</div>")
        
class h1(baseblock):
    def __init__ (self, content, *elems):
        baseblock.__init__(self, elems)
        self.content = content

    def beginblock(self):
        return ("<h1>%s" % (self.content))

    def endblock (self):
        return ("</h1>\n")

class h2(baseblock):
    def __init__ (self, content, *elems):
        baseblock.__init__(self, elems)
        self.content = content

    def beginblock(self):
        return ("<h2>%s" % (self.content))

    def endblock (self):
        return ("</h2>\n")

class h3(baseblock):
    def __init__ (self, content, *elems):
        baseblock.__init__(self, elems)
        self.content = content

    def beginblock(self):
        return ("<h3>%s" % (self.content))

    def endblock (self):
        return ("</h3>\n")

class h4(baseblock):
    def __init__ (self, content, *elems):
        baseblock.__init__(self, elems)
        self.content = content

    def beginblock(self):
        return ("<h4>%s" % (self.content))

    def endblock (self):
        return ("</h4>\n")

class p(baseblock):
    def __init__ (self, *elems):
        baseblock.__init__(self, elems)

    def beginblock(self):
        return ("<p>\n")

    def endblock (self):
        return ("</p>\n")

class text:
    def __init__(self, t):
        self.t = t
    def tohtml(self):
        return (self.t)

class link(baseblock):
    def __init__ (self, dest, *elems):
        baseblock.__init__(self, elems)
        self.dest = dest

    def beginblock(self):
        return ("<a href=\"%s\">" % (self.dest))

    def endblock (self):
        return ("</a>")

class table(baseblock):
    def __init__ (self, params, *elems):
        baseblock.__init__(self, elems)
        self.params = params
        
    def beginblock(self):
        return ("<table %s>" % (self.params))

    def endblock (self):
        return ("</table>\n")

class tr(baseblock):
    def __init__ (self, *elems):
        baseblock.__init__(self, elems)

    def beginblock(self):
        return ("<tr>")

    def endblock (self):
        return ("</tr>\n")

class td(baseblock):

    def __init__ (self, params, *elems):
        baseblock.__init__(self, elems)
        self.params = params
        
    def beginblock(self):
        return ("<td %s>" % (self.params))

    def endblock (self):
        return ("</td>\n")

class th(baseblock):

    def __init__ (self, params, *elems):
        baseblock.__init__(self, elems)
        self.params = params
        
    def beginblock(self):
        return ("<th %s>" % (self.params))

    def endblock (self):
        return ("</th>\n")


class ul(baseblock):
    def __init__ (self, *elems):
        baseblock.__init__(self, elems)

    def beginblock(self):
        return ("<ul>")

    def endblock (self):
        return ("</ul>\n")

class li(baseblock):
    def __init__ (self, *elems):
        baseblock.__init__(self, elems)

    def beginblock(self):
        return ("<li>")

    def endblock (self):
        return ("</li>\n")

class span(baseblock):

    def __init__ (self, cl, *elems):
        baseblock.__init__(self, elems)
        self.cl = cl
        
    def beginblock(self):
        return ("<span class=\"%s\">" % (self.cl))

    def endblock (self):
        return ("</span>\n")
